import 'package:flutter/material.dart';
import 'package:flutterdemo/pages/tab1/dialogAndSheet/bottom_sheet.dart';
import 'package:flutterdemo/pages/tab1/dialogAndSheet/dialogsheet.dart';
import 'package:flutterdemo/pages/tab1/dialogAndSheet/top_sheet.dart';
import 'package:flutterdemo/pages/tab1/dialogAndSheet/top_snackBar.dart';
import 'layer_dialog.dart';

// 枚举类型
enum Options { A, B, C }
enum Alertres { OK, Cancel }

class DialogAndSheet extends StatefulWidget {
  DialogAndSheet({Key key}) : super(key: key);

  _DialogAndSheetState createState() => _DialogAndSheetState();
}

class _DialogAndSheetState extends State<DialogAndSheet> {
  String _currentmenuitem;
  //
  handleBottomSheet() async {
    final res = await showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return new Stack(
            alignment: Alignment.bottomCenter,
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: 15.0, right: 15.0),
                color: Colors.black54,
                width: double.infinity,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12.0),
                      ),
                      child: Column(
                        children: <Widget>[
                          new ListTile(
                            title: Center(
                              child: Text(
                                '选择质押数量 (VCF)',
                                style: TextStyle(
                                  color: Color.fromRGBO(143, 142, 148, 1),
                                  fontSize: 14.0,
                                ),
                              ),
                            ),
                            onTap: () {},
                          ),
                          Container(
                            height: 0.5,
                            width: double.infinity,
                            color: Color.fromRGBO(77, 110, 255, 0.2),
                          ),
                          new ListTile(
                            title: Container(
                              margin: EdgeInsets.only(left: 20.0, right: 10.0),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  new Text(
                                    "40,000",
                                    style: TextStyle(
                                      color: Color.fromRGBO(72, 69, 114, 1),
                                      fontSize: 22.0,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                  new Text(
                                    "2份算力",
                                    style: TextStyle(
                                      color: Color.fromRGBO(148, 146, 169, 1),
                                      fontSize: 14.0,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () async {
                              Navigator.pop(context, {
                                "s": '40,000',
                                "v": 40000,
                              });
                            },
                          ),
                          Container(
                            height: 0.5,
                            width: double.infinity,
                            color: Color.fromRGBO(77, 110, 255, 0.2),
                          ),
                          new ListTile(
                            title: Container(
                              margin: EdgeInsets.only(left: 20.0, right: 10.0),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  new Text(
                                    "80,000",
                                    style: TextStyle(
                                      color: Color.fromRGBO(72, 69, 114, 1),
                                      fontSize: 22.0,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                  new Text(
                                    "4份算力",
                                    style: TextStyle(
                                      color: Color.fromRGBO(148, 146, 169, 1),
                                      fontSize: 14.0,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () async {
                              Navigator.pop(context, {
                                "s": '80,000',
                                "v": 80000,
                              });
                            },
                          ),
                          Container(
                            height: 0.5,
                            width: double.infinity,
                            color: Color.fromRGBO(77, 110, 255, 0.2),
                          ),
                          new ListTile(
                            title: Container(
                              margin: EdgeInsets.only(left: 20.0, right: 10.0),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  new Text(
                                    "100,000",
                                    style: TextStyle(
                                      color: Color.fromRGBO(72, 69, 114, 1),
                                      fontSize: 22.0,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                  new Text(
                                    "5份算力",
                                    style: TextStyle(
                                      color: Color.fromRGBO(148, 146, 169, 1),
                                      fontSize: 14.0,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              Navigator.pop(context, {
                                "s": '100,000',
                                "v": 100000,
                              });
                            },
                          ),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 10.0,
                    ),
                    new ListTile(
                      contentPadding: EdgeInsets.all(0.0),
                      title: Container(
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(12.0),
                        ),
                        height: 57.0,
                        width: double.infinity,
                        child: Center(
                          child: Text(
                            '取消',
                            style: TextStyle(
                              fontSize: 20.0,
                              color: Color.fromRGBO(148, 146, 169, 1),
                            ),
                          ),
                        ),
                      ),
                      onTap: () {
                        Navigator.pop(context);
                      },
                    ),
                    SizedBox(
                      height: 10.0,
                    ),
                  ],
                ),
              ),
            ],
          );
        });

    if (res != null) {
      print('选择了 ${res['v']}');
    } else {
      print('点击了取消');
    }
  }

  handlenormalBottomSheet() {
    showModalBottomSheetCustom(
      context: context,
      builder: (BuildContext context) {
        return DialogSheet();
      },
    ).then((val) {
      print(val);
    });
  }

  // Alert 对话框
  String _alertchice = 'nothing';
  Future _openalertdialog() async {
    final res = await showDialog(
        context: context,
        // 点击浮层不会关闭对话框
        // barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('alert dialog'),
            content: Text('Are you sure about this?'),
            actions: <Widget>[
              FlatButton(
                child: Text('Cancel'),
                onPressed: () {
                  Navigator.pop(context, Alertres.Cancel);
                },
              ),
              FlatButton(
                child: Text('OK'),
                onPressed: () {
                  Navigator.pop(context, Alertres.OK);
                },
              ),
            ],
          );
        });
    switch (res) {
      case Alertres.OK:
        setState(() {
          _alertchice = 'OK';
        });
        break;
      case Alertres.Cancel:
        setState(() {
          _alertchice = 'Cancel';
        });
        break;
      default:
    }
  }

//  botton sheet
  final keys = GlobalKey<ScaffoldState>();
  _openbottomsheet() {
    keys.currentState.showBottomSheet((BuildContext context) {
      return BottomAppBar(
        child: Container(
            height: 150.0,
            width: double.infinity,
            padding: EdgeInsets.all(16.0),
            child: Container(
              child: Column(
                children: <Widget>[
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop(1);
                    },
                    child: Row(
                      children: <Widget>[
                        Icon(Icons.ac_unit),
                        SizedBox(
                          width: 16.0,
                        ),
                        Text('data1'),
                        Expanded(
                          child: Text(
                            'Expanded1',
                            textAlign: TextAlign.right,
                          ),
                        ),
                      ],
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop(2);
                    },
                    child: Row(
                      children: <Widget>[
                        Icon(Icons.ac_unit),
                        SizedBox(
                          width: 16.0,
                        ),
                        Text('data2'),
                        Expanded(
                          child: Text(
                            'Expanded2',
                            textAlign: TextAlign.right,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            )),
      );
    });
  }

// dialog 对话框
  String _choice = 'nothing';

  Future _opensimpledialog() async {
    final option = await showDialog(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: Text('simpledialog1'),
            children: <Widget>[
              SimpleDialogOption(
                child: Text('option A'),
                onPressed: () {
                  Navigator.pop(context, Options.A);
                },
              ),
              SimpleDialogOption(
                child: Text('option B'),
                onPressed: () {
                  Navigator.pop(context, Options.B);
                },
              ),
              SimpleDialogOption(
                child: Text('option C'),
                onPressed: () {
                  Navigator.pop(context, Options.C);
                },
              ),
            ],
          );
        });
    switch (option) {
      case Options.A:
        setState(() {
          _choice = 'A';
        });
        break;
      case Options.B:
        setState(() {
          _choice = 'B';
        });
        break;
      case Options.C:
        setState(() {
          _choice = 'C';
        });
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: keys,
      appBar: AppBar(
        title: Text('DialogAndSheet'),
      ),
      body: ListView(
        children: <Widget>[
          ListTile(
            title: Text('类似IOS showModalBottomSheet'),
            onTap: handleBottomSheet,
          ),
          ListTile(
            title: Text('normal showModalBottomSheet'),
            onTap: handlenormalBottomSheet,
          ),
          ListTile(
            title: Text('showModalTopSheet'),
            onTap: () {
              showModalTopSheet(
                context: context,
                builder: (BuildContext context) {
                  return DialogSheet();
                },
              );
            },
          ),
          ListTile(
            title: Text('open alert dialog 选择了 $_alertchice'),
            onTap: _openalertdialog,
          ),
          ListTile(
            title: Text('open bottom sheet'),
            onTap: _openbottomsheet,
          ),
          ListTile(
            title: Text('open simple dialog 选择了 $_choice'),
            onTap: _opensimpledialog,
          ),
          ListTile(
            title: Text('open layer dialog'),
            onTap: () {
              showDialog<Null>(
                context: context,
                barrierDismissible: false,
                builder: (BuildContext context) {
                  return LayerDialog(
                    title: '待领取收益',
                    infowidget: InnerInfo0(),
                  );
                },
              );
            },
          ),
          ListTile(
            title: Text('show bottom SnackBar'),
            onTap: () {
              keys.currentState.showSnackBar(SnackBar(
                content: Text('已复制到剪切板'),
                duration: Duration(milliseconds: 1500),
                action: SnackBarAction(
                  label: 'ok',
                  onPressed: () {},
                ),
              ));
            },
          ),
          ListTile(
            title: Text('类似 top SnackBar'),
            onTap: () {
              showTopSnackBar(
                context: context,
                builder: (BuildContext context) {
                  return Container(
                    height: 50.0,
                    color: Colors.black,
                    child: Center(
                      child: Text(
                        '已复制到剪切板',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.0,
                        ),
                      ),
                    ),
                  );
                },
              );
              Future.delayed(Duration(seconds: 1), () {
                Navigator.of(context).pop();
              });
            },
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              SizedBox(
                width: 15.0,
              ),
              PopupMenuButton(
                initialValue: '$_currentmenuitem',
                tooltip: 'data',
                elevation: 0.0,
                // icon: Icon(Icons.add),
                child: Container(
                  // height: 30.0,
                  // width: 50.0,
                  // color: Colors.red,
                  child: Text('PopupMenuButton 选择了 $_currentmenuitem'),
                ),
                onSelected: (v) {
                  print('onSelected : $v');
                  setState(() {
                    _currentmenuitem = v;
                  });
                },
                onCanceled: () {
                  print('onCanceled');
                },
                itemBuilder: (BuildContext context) {
                  return [
                    PopupMenuItem(
                      value: 'item1',
                      child: Text('item1'),
                    ),
                    PopupMenuItem(
                      value: 'item2',
                      child: Text('item2'),
                    ),
                    PopupMenuItem(
                      value: 'item3',
                      child: Text('item3'),
                    ),
                  ];
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}

class InnerInfo0 extends StatelessWidget {
  const InnerInfo0({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 12.0),
      child: Column(
        children: <Widget>[
          Text(
            '        VCF是VCfund基金发行的生态通证，用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖用户可以使用ETH通过共振获得VCF，激活VCid并持有VCF每日获得各种生态奖VCF每日获得各种生态奖奖励。共振池内的ETH100%返还社区用户，基金生态投资业务所产生的80%收益将于社区用户共享。在VCF生态内，用户每日可获得ETH以及VCF收益，每日中午12点统一发放前一天的ETH以及VCF收益。邀请好友参与共振，质押VCF，参与裂变竞赛等活动都能获得每日ETH奖励。邀请好友激活VCid，质押成为超级合伙人能获得每日VCF奖励。平台每日为生态建设者提供了各种丰富的ETH以及VCF奖励，付出越多回报越大！',
            style: TextStyle(
              color: Color.fromRGBO(72, 69, 114, 1),
              fontSize: 14.0,
              height: 1.2,
            ),
          ),
        ],
      ),
    );
  }
}
